<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Logs extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 50;
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->load->language('common', $this->_lang);
		$this->_template_f = $this->config->item('template_f');
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > -1)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('logs/logs_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('logs/logs', $this->_lang);
	}
	
	function index()
	{
		$this->show();
	}
	
	function email()
	{
		$type = 'action';
		
		$data = array();
		$data['lang'] = $this->lang->line('logs_lang');
		//Lay cac tham so filter
		$data['el_type'] = $this->input->get('el_type',true);
		if($data['el_type'] == '') $data['el_type'] = -1;
		else $data['el_type'] = (int)$data['el_type'];
		
		$data['el_usr'] = (int)$this->input->get('el_usr',true);
		// End
		
		//Phan trang
		$limit = 20;
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		//End
		
		$data['email_log'] = $this->logs_model->get_email_log_list($data['el_type'], $data['el_usr'], $page,$limit);
		$data['total_page'] = $data['email_log']['pages'];
		$data['email_log'] = $data['email_log']['items'];
		$data['usr_list'] = $this->logs_model->get_users_list();
		$data['page'] = $page;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'logs/email', $data);
		
		$this->_loadFooter();
	}
	
	function show()
	{
		$type = 'action';
		
		$data = array();
		$data['lang'] = $this->lang->line('logs_lang');
		//Lay cac tham so filter
		$data['el_type'] = $this->input->get('el_type',true);
		if($data['el_type'] == '') $data['el_type'] = -1;
		else $data['el_type'] = (int)$data['el_type'];
		
		$data['el_usr'] = trim($this->input->get('el_usr',true));
		$data['el_id'] = (int)$this->input->get('el_id', true);
		$data['el_date'] = $this->input->get('el_date',true);
		// End
		
		//Phan trang
		$limit = 20;
		$page = (int)$this->input->get('page',true);
		if($page <= 0) $page = 1;
		//End
		
		$data['email_log'] = $this->logs_model->get_inform_list($data['el_type'],$data['el_usr'],$data['el_id'],$data['el_date'],$page,$limit);
		$data['total_page'] = $data['email_log']->pages;
		$data['email_log'] = $data['email_log']->items;
		$data['page'] = $page;
		
		$data['usr_list'] = $this->logs_model->get_users_list();
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'logs/show', $data);
		
		$this->_loadFooter();
		
	}
	
	function payment()
	{
		return;
		$uid = (int)$this->input->get('uid', true);
		
		$type = trim($this->input->get('type', true));
		if($type == '') $type = -1;
		$type = (int)$type;
		
		$status = trim($this->input->get('status', true));
		if($status == '') $status = -1;
		$status = (int)$status;
		
		$page = (int)$this->input->get('page', true);
		if($page < 1) $page = 1;
		
		$uinfo = $this->budget_model->get_user_info($uid);
		$logs = $this->budget_model->get_payment_logs($uid, $uinfo->role, $type, $status, $page, 20);
		
		$data = array();
		$data['uinfo'] = $uinfo;
		$data['info'] = $logs->info;
		$data['items'] = $logs->items;
		$data['lang'] = $this->lang->line('budget');
		$data['page'] = $page;
		$data['totalPage'] = $logs->totalPage;
		$data['type'] = $type;
		$data['status'] = $status;
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . '/budget/user', $data);
		$this->_loadFooter();
	}
	
	function check_soha()
	{
		$order_code = trim($this->input->get('code'));
		$info = $this->logs_model->payment_log_info_by_key($order_code);
		//Xac dinh kieu order bang the atm hay the cao dien thoai
		$queryType = 'query';
		if(strpos($info->typepay, ' - Card') !== false || strpos($info->typepay, ' - Mobile card') !== false)
		{
			$queryType = 'queryMobileCard';
		}
		
		$arrReturn = array();
		$this->load->library('pg_checkout');
		if($order_code != '')
		{
			$arrReturn = $this->pg_checkout->queryOrderStatus($order_code, $queryType);
		}

		$data = array();
		$data['arrReturn'] = $arrReturn;
		
		$this->_loadHeader();
		$this->_loadMenu();
		
		$this->load->view($this->_template_f.'logs/check_soha', $data);
		
		$this->_loadFooter();
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function mcweekly_service()
	{
		
		// load header
		$this->_loadHeader('Muchung weekly service logs');
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$cPage = trim($this->input->get('page'));
		$cPage = is_numeric($cPage) && $cPage > 0 ? $cPage : 1;
		
		
		$data = array();
		$data['lang'] = $this->lang->line('logs_lang');
		//Lay cac tham so filter
		$data['el_type'] = 30;	
		$data['el_date'] = $this->input->get('el_date',true);
		
		$data['email_log'] = $this->logs_model->get_inform_list($data['el_type'], '', 0, $data['el_date'], $cPage, 30);
		$data['total_page'] = $data['email_log']->pages;
		$data['email_log'] = $data['email_log']->items;
		$data['page'] = $cPage;
		
		$this->load->view($this->_template_f . 'logs/mcweekly_service_view', $data);
		
		// load footer
		$this->_loadFooter();
	}
}